﻿/****************************** Module Header ******************************\
 * Module Name:  SessionDataTable.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is a basic class for the SessionDataTable.  It supplies common public
 * methods to process a internet request, such as Request processing and so on. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Web;

namespace UCF.Core.Web
{
    /// <summary>
    /// 会话字典对象
    /// </summary>
    public class SessionDictionary
    {
        /// <summary>
        /// 当前会话名称
        /// </summary>
        private string sessionName;

        /// <summary>
        /// 信息存储类型
        /// <para>1=Cookie,2=Session，默认是Session</para>
        /// </summary>
        public StorageType LoginStorageType { get; set; }

        /// <summary>
        /// 数据集
        /// </summary>
        public Dictionary<string, string> DataSet { get; set; }
        
        /// <summary>
        /// 构造方法
        /// </summary>
        public SessionDictionary(string name)
        {
            this.sessionName = name;
            this.DataSet = this.Get();
        }

        /// <summary>
        /// 登录信息存储类型
        /// </summary>
        public enum StorageType
        {
            /// <summary>
            /// 使用Cookie存储信息
            /// </summary>
            Cookie = 1,

            /// <summary>
            /// 使用Session存储信息
            /// </summary>
            Session = 2
        }

        /// <summary>
        /// 获取字典
        /// </summary>
        /// <returns></returns>
        public  Dictionary<string, string> Get()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            string v = this.GetStoreageData();
            if (v.Length == 0) return dict;
            v = HttpContext.Current.Server.UrlDecode(v);
            return v.ToDictionary();
        }

        /// <summary>
        /// 保存登录信息
        /// </summary>
        /// <param name="dict"></param>
        public void Save(Dictionary<string, string> dict)
        {
            this.SetStoreageData(dict.ToStringKeyValue());
        }

        /// <summary>
        /// 保存登录信息
        /// </summary>
        public void Save()
        {
            this.SetStoreageData(this.DataSet.ToStringKeyValue());
        }

        /// <summary>
        /// 更新用户状态信息
        /// </summary>
        /// <param name="k"></param>
        /// <param name="v"></param>
        public void Update(string k, string v)
        {
            Dictionary<string, string> dict = this.Get();
            dict.Modify(k, v);
            this.Save(dict);
        }

        /// <summary>
        /// 注销数据
        /// </summary>
        public void Clear()
        {
            if (LoginStorageType == StorageType.Session)
            {
                HttpContext.Current.Session[this.sessionName] = null;
            }
            else
            {
                HttpContext.Current.Response.Cookies[this.sessionName].Value = string.Empty;
                HttpContext.Current.Response.Cookies[this.sessionName].Expires = DateTime.Now.AddDays(-1);
            }
        }

        /// <summary>
        /// 读取值
        /// </summary>
        /// <returns></returns>
        private string GetStoreageData()
        {
            if (LoginStorageType == StorageType.Session)
            {
                return HttpContext.Current.Session[this.sessionName] == null ? "" : HttpContext.Current.Session[this.sessionName].ToString();
            }
            else
            {
                HttpCookie cok = HttpContext.Current.Request.Cookies[this.sessionName];
                return cok == null ? "" : cok.Value;
            }
        }

        /// <summary>
        /// 存储值
        /// </summary>
        /// <param name="v"></param>
        private void SetStoreageData(string v)
        {
            if (LoginStorageType == StorageType.Session)
            {
                HttpContext.Current.Session[this.sessionName] = v;
            }
            else
            {
                HttpContext.Current.Response.Cookies[this.sessionName].Value = v;
            }
        }

        /// <summary>
        /// 添加字段名称
        /// </summary>
        /// <param name="names"></param>
        public void AddFields(params string[] names)
        {
            this.DataSet.AddKeys<string, string>(names);
        }

        /// <summary>
        /// 添加值
        /// </summary>
        /// <param name="values"></param>
        public void AddValues(params string[] values)
        {
            this.DataSet.AddValues<string, string>(values);
        }

    }
}
